<?php
namespace app\staff\model;
use app\common\model\Common;
use think\Db;
class GoodsCount extends Common
{
	protected $table = 'mps_order_goods';
	protected $append = [];

	/**
	 * 员工产品销售业绩-列表
	 * @param  [type] $where  [description]
	 * @param  [type] $page   [description]
	 * @param  [type] $row    [description]
	 * @param  [type] $order  [description]
	 * @param  [type] $search [description]
	 * @return [type]         [description]
	 */
	public function getGoodsCount($where, $order, $page, $row, $search)
	{
		if($where === false) return [];
		return $this->alias('og')->join('mps_order o','og.order_id = o.order_id','LEFT')
		->field('og.order_goods_id,og.goods_name,og.unit,og.goods_sn,ifnull(sum(og.number),0) as number,ifnull(sum(og.profit_t_money),0) as profit_t_money,ifnull(sum(og.turnover_t_money),0) as turnover_t_money,ifnull(sum(og.goods_money),0) as goods_money,ifnull(sum(og.profit_money),0) as profit_money,ifnull(sum(og.cost_money),0) as cost_money,ifnull((case is_show when 1 then sum(og.goods_money) end),0) as refund_money,ifnull((case is_show when 2 then sum(og.goods_money) end),0) as recal_money')
		->where($where)->where($search)
		->page($page, $row)->order($order)->group('og.goods_sn')->select();
	}
	/**
	 * 员工产品销售业绩-数量
	 * @param  [type] $where  [description]
	 * @param  [type] $order  [description]
	 * @param  [type] $search [description]
	 * @return [type]         [description]
	 */
	public function tjGoodsCount($where, $order, $search)
	{
		if($where === false) return 0;
		$sql = $this->alias('og')->join('mps_order o','og.order_id = o.order_id','LEFT')
		->field('og.goods_sn')
		->where($where)->where($search)->order($order)->group('og.goods_sn')->select(false);
		$m = "($sql) as a";
		return Db::table($m)->count();
	}
	/**
	 * 员工产品销售业绩-统计
	 * @param  [type] $where  [description]
	 * @param  [type] $order  [description]
	 * @param  [type] $search [description]
	 * @return [type]         [description]
	 */
	public function allGoodsCount($where, $order, $search)
	{
		if($where === false) return ['goods_money'=>0,'cost_money'=>0,'refund_money'=>0,'recal_money'=>0,'profit_money'=>0,'profit_t_money'=>0,'turnover_t_money'=>0];
		return $this->alias('og')->join('mps_order o','og.order_id = o.order_id','LEFT')
		->field('og.order_goods_id,og.goods_name,og.unit,og.goods_sn,ifnull(sum(og.number),0) as number,ifnull(sum(og.profit_t_money),0) as profit_t_money,ifnull(sum(og.turnover_t_money),0) as turnover_t_money,ifnull(sum(og.goods_money),0) as goods_money,ifnull(sum(og.profit_money),0) as profit_money,ifnull(sum(og.cost_money),0) as cost_money,ifnull((case is_show when 1 then sum(og.goods_money) end),0) as refund_money,ifnull((case is_show when 2 then sum(og.goods_money) end),0) as recal_money')
		->where($where)->where($search)->page($page, $row)->order($order)->select();
	}

	/****************************************** 私有 ***********************************************/

	
}